package com.mdnote.jianzhi_offer.linked_list;

import java.util.Stack;

/**
 * @author Rhythm-2019
 * @version 1.0
 * @date 2020/10/11
 * @description 剑指 Offer 06. 从尾到头打印链表
 */
public class Offer06 {

    public int[] reversePrint(ListNode head) {

        if (head == null) {
            return new int[0];
        }

        Stack<Integer> stack = new Stack<>();
        ListNode cur = head;
        int index = 0;
        while (cur != null) {
            stack.push(cur.val);
            cur = cur.next;
        }

        int[] res = new int[stack.size()];
        while (!stack.isEmpty()) {
            res[index++] = stack.pop();
        }
        return res;
    }

    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }
}
